from flask import Flask, request, jsonify

from app.model.experimentModel import Experiments


def register(app: Flask):
    @app.route("/api/test", methods=["POST"])
    def test():
        return "已经和后端连调成功"

    @app.route("/api/search", methods=["POST"])
    def search():
        value = request.form.get("value")
        exps = []
        if value:
            exps = Experiments.objects(name__contains=value)
        return jsonify(list(map(lambda item: {
            "name": item.name,
            "goals": '、'.join(map(lambda it: it.name, item.goals)),
            "groups": list(map(lambda it: {
                "condition": it.condition,
                "line": it.line,
                "result": it.result
            }, item.groups)),
            "author": item.doc_meta.author,
        }, exps)))
